﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace LottoMat
{
    public partial class FormLotto : XtraForm
    {
        public FormLotto()
        {
            InitializeComponent();
        }
        int[] max = new int[6] { 44, 45, 46, 47, 48, 49 };
        int maxCount = 1;
        private void FormLotto_Load(object sender, EventArgs e)
        {
            try
            {
                dataSetLotto1.ReadXml("lotto.xml", XmlReadMode.InferSchema);
            }
            catch
            {

            }
            if (dataSetLotto1.Lotto.Rows.Count > 0)
                modeProvider1.CurrentMode = ProgNetDxComponents.ExtenderProviders.FormMode.Edit;
            int tmp = 1;
            for (int i= max[5]; i > max[5] - 6; i--)
            {
                maxCount *= i;
                maxCount /= tmp;
                tmp++;
            }
            progressBarControl1.Properties.Maximum = maxCount;
            progressBarControl1.EditValue = dataSetLotto1.Lotto.Rows.Count;
            PrintInfo();
        }
        protected override void OnFormClosed(FormClosedEventArgs e)
        {
            base.OnFormClosed(e);
            dataSetLotto1.WriteXml("lotto.xml", XmlWriteMode.WriteSchema);
        }

        private void btnWznow_Click(object sender, EventArgs e)
        {
            cancel = false;
            modeProvider1.CurrentMode = ProgNetDxComponents.ExtenderProviders.FormMode.Read;
            Generuj();
        }
        private void btnGenerujKombinacje_Click(object sender, EventArgs e)
        {
            cancel = false;
            modeProvider1.CurrentMode = ProgNetDxComponents.ExtenderProviders.FormMode.Read;
            Generuj();
        }

        bool cancel = false;

        private void Generuj()
        {
            
            //int[] max = new int[6] { 10, 11, 12, 13, 14, 15 };
            int[] liczby = new int[6] { 1, 2, 3, 4, 5, 5 };
           
            
            if (dataSetLotto1.Lotto.Rows.Count > 0)
            {
                DataRow last =  dataSetLotto1.Lotto.Rows[dataSetLotto1.Lotto.Rows.Count - 1];
                for (int i=0; i < 6; i++)
                    liczby[i] = last[i+1].AsInt();
            }
            bool mod = true;
            object[] obj = new object[7];

            while (true)
            {
                mod = false;
                for (int i=5; i >=0 ; i--)
                {
                    if (liczby[i] < max[i])
                    {
                        liczby[i]++;
                        if (i < 5)
                        {
                            for (int j=i+1; j < 6; j++)
                            {
                                liczby[j] = liczby[j - 1] + 1;
                            }
                        }
                        mod = true;
                        break;
                    }
                }
                obj[0] = dataSetLotto1.Lotto.Rows.Count+1;
                for (int i=0; i < 6; i++)
                    obj[i+1] = liczby[i];
                dataSetLotto1.Lotto.Rows.Add(obj);
                PrintInfo();
                progressBarControl1.PerformStep();
                
                //System.Threading.Thread.Sleep(10);
                Application.DoEvents();
                //System.Threading.Thread.Sleep(10);
                if (!mod)
                    break;

                if (cancel)
                    break;
            }
            modeProvider1.CurrentMode = ProgNetDxComponents.ExtenderProviders.FormMode.Edit;

        }
        private void PrintInfo(){
             labelKolejna.Text = "Numer Kombinacji: " + dataSetLotto1.Lotto.Rows.Count + " / " + maxCount;
        }
        private void bntAnuluj_Click(object sender, EventArgs e)
        {
            cancel = true;
        }

        

        
    }
}
